package com.seagox.chat.auth.serivce.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seagox.chat.auth.serivce.IAuthService;
import com.seagox.chat.chat.entity.BaseUser;
import com.seagox.chat.chat.mapper.UserMapper;
import com.seagox.chat.common.ResultCode;
import com.seagox.chat.common.ResultData;
import com.seagox.chat.security.JwtTokenUtils;
import com.seagox.chat.util.EncryptUtils;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthService implements IAuthService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public ResultData login(String account, String password) {
		LambdaQueryWrapper<BaseUser> qw = new LambdaQueryWrapper<>();
		qw.or((wrapper) -> {
			wrapper.eq(BaseUser::getPhone, account).or();
			wrapper.eq(BaseUser::getEmail, account);
		});
		qw.eq(BaseUser::getStatus, 1);
		BaseUser user = userMapper.selectOne(qw);
		if (user == null) {
			return ResultData.warn(ResultCode.PARAMETER_ERROR, "用户名或密码错误");
		} else {
			boolean result = EncryptUtils.checkpw(password, user.getPassword());
			if (!result) {
				return ResultData.warn(ResultCode.PARAMETER_ERROR, "用户名或密码错误");
			}
			Map<String, Object> payload = new HashMap<String, Object>();
			payload.put("userId", user.getId());
			String accessToken = JwtTokenUtils.sign(payload, JwtTokenUtils.EXPIRATION);
			payload.put("accessToken", JwtTokenUtils.TOKENHEAD + accessToken);
			payload.put("name", user.getName());
			payload.put("avatar", user.getAvatar());
			payload.put("type", user.getType());
			return ResultData.success(payload);
		}
	}
	
	@Override
    public ResultData verifyLogin(String account, String noncestr, String timestamp, String sign) {
        String key = "yVwlsbIrY3q22EnoYYM4nR5zqTmqed05";
        String ratioSign = EncryptUtils.md5Encode("account=" + account + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&key=" + key).toUpperCase();
        if (ratioSign.equals(sign)) {
        	LambdaQueryWrapper<BaseUser> qw = new LambdaQueryWrapper<>();
    		qw.or((wrapper) -> {
    			wrapper.eq(BaseUser::getPhone, account).or();
    			wrapper.eq(BaseUser::getEmail, account);
    		});
    		qw.eq(BaseUser::getStatus, 1);
    		BaseUser user = userMapper.selectOne(qw);
            if (user == null) {
                return ResultData.warn(ResultCode.SIGN_ERROR, "用户不存在");
            } else {
    			Map<String, Object> payload = new HashMap<String, Object>();
    			payload.put("userId", user.getId());
    			String accessToken = JwtTokenUtils.sign(payload, JwtTokenUtils.EXPIRATION);
    			payload.put("accessToken", JwtTokenUtils.TOKENHEAD + accessToken);
    			payload.put("name", user.getName());
    			payload.put("avatar", user.getAvatar());
    			payload.put("type", user.getType());
    			return ResultData.success(payload);
            }
        } else {
            return ResultData.warn(ResultCode.SIGN_ERROR, "签名错误");
        }
    }
    
}
